给定源程序:
#include <stdio.h>
#include <alloc.h>
#include <string.h>
#define N 10
typedef struct ss
{ char num[10];
int s;
} STU;
STU *fun(STU a[], int m)
{ STU b[N], *t;
int i,j,k;
t=(STU *)calloc(sizeof(STU),m)
for(i=0; i<N; i++) b[i]=a[i];
for(k=0; k<m; k++)
{ for(i=j=0; i<N; i++)
if(b[i].s > b[j].s) j=i;
t(k)=b(j);
b[j].s=0;
}
return t;
}
outresult(STU a[], FILE *pf)
{ int i;
for(i=0; i<N; i++)
fprintf(pf,"No = %s Mark = %d\n", a[i].num,a[i].s);
fprintf(pf,"\n\n");
}
main()
{ STU a[N]={ {"A01",81},{"A02",89},{"A03",66},{"A04",87},{"A05",77},
{"A06",90},{"A07",79},{"A08",61},{"A09",80},{"A10",71} };
STU *pOrder;
int i, m;
printf("***** The Original data *****\n");
outresult(a, stdout);
printf("\nGive the number of the students who have better score: ");
scanf("%d",&m);
while( m>10 )
{ printf("\nGive the number of the students who have better score: ");
scanf("%d",&m);
}
pOrder=fun(a,m);
printf("***** THE RESULT *****\n");
printf("The top :\n");
for(i=0; i<m; i++)
printf(" %s %d\n",pOrder[i].num , pOrder[i].s);
free(pOrder);
}
解题思路:
第一处: 语句最后缺少分号。
第二处: 应该使用方括号,而不是圆括号。
像此类,使用编译,即可发现。
***************************************************
请编写函数fun, 函数的功能是: 删去一维数组中所有相同的数, 使之只剩一个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。
例如, 一维数组中的数据是: 2 2 2 3 4 4 5 6 6 6 6 7 7 8 9 9 10 10 10。
删除后,数组中的内容应该是: 2 3 4 5 6 7 8 9 10。
注意:部分源程序在文件PROG1.C中。
请勿改动主函数main和其它函数中的任何内容, 仅在函数fun的花括号中填入你编写的若干语句。